package com.zlyx.easy.web.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;

/**
 * @Auth 赵光
 * @Describle
 * @2019年6月12日 下午6:48:27
 */
@Configuration
@ConditionalOnClass(DataSourceConfig.class)
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceConfiguration {

	@Autowired
	DataSourceProperties properties;

	@Bean
	@ConditionalOnMissingBean
	public DataSourceConfig getDataSource() {
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl(properties.getUrl());
		dsc.setDriverName(properties.getDriverClassName());
		dsc.setUsername(properties.getUsername());
		dsc.setPassword(properties.getPassword());
		return dsc;
	}

	@Bean
	@ConditionalOnMissingBean
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSource dataSource() {
		return new DruidDataSource();
	}
}
